using System.Collections;
using UnityEngine;

public class InGameTutorialSellectNear : InGameTutorialObject
{
	[SerializeField]
	private Transform[] objects;

	[SerializeField]
	private float UpdateTime = 5f;

	private bool startedCorotune;

	private Transform target;

	private Transform lasttarget;

	public override bool OnStepStart(string name)
	{
		if (base.OnStepStart(name))
		{
			target = null;
			lasttarget = null;
			StartCoroutine(UpdateTargets(name));
			return true;
		}
		return false;
	}

	private IEnumerator UpdateTargets(string name)
	{
		for (int j = 1; j < objects.Length; j++)
		{
			Renderer rend2 = objects[j].GetComponentInChildren<Renderer>();
			if (rend2 != null && rend2.enabled)
			{
				target = objects[j];
				break;
			}
		}
		startedCorotune = false;
		for (int k = 0; k < values.Length; k++)
		{
			if (values[k].started)
			{
				startedCorotune = true;
				break;
			}
		}
		while (startedCorotune)
		{
			if (GameController.instance != null)
			{
				for (int i = 0; i < objects.Length; i++)
				{
					Renderer rend = objects[i].GetComponentInChildren<Renderer>();
					if (rend != null && rend.enabled && Vector3.Distance(objects[i].position, GameController.instance.myPlayer.transform.position) < Vector2.Distance(target.position, GameController.instance.myPlayer.transform.position))
					{
						target = objects[i];
					}
				}
			}
			if (target != lasttarget)
			{
				PointerController.instance.RemovePointer(lasttarget);
				PointerController.instance.AddPointer(target, 0f, "iconMap_Red Cercle");
				lasttarget = target;
			}
			yield return StartCoroutine(BCWUtils.WaitForRealSeconds(UpdateTime));
		}
	}

	private void Update()
	{
		bool flag = false;
		for (int i = 0; i < values.Length; i++)
		{
			if (values[i].started)
			{
				flag = true;
				break;
			}
		}
		if (!(target == null) && flag && !(Vector2.Distance(new Vector2(target.position.x, target.position.z), new Vector2(GameController.instance.myPlayer.transform.position.x, GameController.instance.myPlayer.transform.position.z)) < 3.5f))
		{
		}
	}

	public override void OnEnd()
	{
		base.OnEnd();
		startedCorotune = false;
		for (int i = 0; i < values.Length; i++)
		{
			startedCorotune = false;
		}
		StopCoroutine(UpdateTargets(string.Empty));
		PointerController.instance.RemovePointer(target);
	}
}
